<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/11
 * Time: 15:28
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class SeminarTemplateCategoryGoodsModel
 * @package SDXApi\Model
 */
class SeminarTemplateCategoryGoodsModel extends Model
{
	/**
	 * @var string
	 */
	protected $tableName = 'seminar_template_category_goods';

	/**
	 * Created by Jerry
	 * @param $id
	 * @return mixed
	 */
	public function findCategoryByTemplateId($id)
	{
		$where = array(
			'template_id'	=>	$id,
		);

		return $this->where($where)->find();
	}


    /**
     * @author Jerry
     * @param $category_id
     * @param $goods_where
     * @param int $wid
     * @param int $is_hide_promote
     * @return bool|mixed
     */
	public function findCategoryGoodsByCategoryId($category_id, $goods_where, $wid = 8, $is_hide_promote = 0)
	{

        $promoteTime = gmtime();

//        销量
		$categoryShowType = array(
			1	=>	20,
			2	=>	30,
			3	=>	50,
            16  =>  10,
		);

//		新品
		$categoryNewType = array(
			4	=>	20,
			5	=>	30,
			6	=>	50,
            17  =>  10,
		);

//		精品
		$categoryIsBest = array(
			7	=>	20,
			8	=>	30,
			9	=>	50,
            18  =>  10,
		);

//		热销
		$categoryIsHot = array(
			10	=>	20,
			11	=>	30,
			12	=>	50,
            19  =>  10,
		);

//		新品
		$categoryIsNew = array(
			13	=>	20,
			14	=>	30,
			15	=>	50,
            20  =>  10,
		);

		$where = false;

//		销量
		if (array_key_exists($goods_where, $categoryShowType)) {
			$where = true;

            $field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.promote_user_rank,gs.practical_num';

            $isBestWhere = array(
                'g.cat_id'				=>	$category_id,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                'g.original_img'		=>	array('NEQ', ''),
            );


            if ($is_hide_promote) {
                $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

            $sql = $this
                ->table('sdx_goods_wid_' . $wid)
                ->alias('g')
                ->field($field)
                ->join('LEFT JOIN (select goods_id, count(goods_id) as num from sdx_comment_goods GROUP BY goods_id ) as cg on g.goods_id = cg.goods_id ')
                ->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id and gs.wid = ' . $wid)
                ->where($isBestWhere)
                ->order('cg.num desc')
                ->limit($categoryShowType[$goods_where])
                ->select(false);
		}

//		新添加
		if (array_key_exists($goods_where, $categoryNewType)) {
			$where = true;

            $field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.promote_user_rank,gs.practical_num';

            $isBestWhere = array(
                'g.cat_id'				=>	$category_id,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                'g.original_img'		=>	array('NEQ', ''),
            );

            if ($is_hide_promote) {
                $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

            $sql = $this
                ->table('sdx_goods_wid_' . $wid)
                ->alias('g')
                ->field($field)
                ->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id and gs.wid = ' . $wid)
                ->where($isBestWhere)
                ->order('g.add_time desc')
                ->limit($categoryNewType[$goods_where])
                ->select(false);
		}

//		精品
		if (array_key_exists($goods_where, $categoryIsBest)) {
			$where = true;

			$time = time();

			$field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.promote_user_rank,gs.practical_num';

			$isBestWhere = array(
				'g.cat_id'				=>	$category_id,
				'g.is_best'				=>	1,
				'gg.is_best_start_time'	=>	array('LT', $time),
				'gg.is_best_end_time'	=>	array('GT', $time),
				'g.is_delete'			=>	0,
				'g.is_on_sale'			=>	1,
				'g.original_img'		=>	array('NEQ', ''),
			);

			if ($is_hide_promote) {
			    $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

			$sql = $this
				->table('sdx_goods_wid_' . $wid)
				->alias('g')
				->field($field)
				->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id and gs.wid = ' . $wid)
                ->join('LEFT JOIN __GOODS__ AS gg ON g.goods_id = gs.goods_id')
				->where($isBestWhere)
				->limit($categoryIsBest[$goods_where])
				->select(false);
		}

//		新品
		if (array_key_exists($goods_where, $categoryIsNew)) {
			$where = true;

			$time = time();

			$field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.promote_user_rank,gs.practical_num';

			$isBestWhere = array(
				'g.cat_id'				=>	$category_id,
				'g.is_new'				=>	1,
				'gg.is_new_start_time'	=>	array('LT', $time),
				'gg.is_new_end_time'	=>	array('GT', $time),
				'g.is_delete'			=>	0,
				'g.is_on_sale'			=>	1,
				'g.original_img'		=>	array('NEQ', ''),
			);

            if ($is_hide_promote) {
                $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

			$sql = $this
				->table('sdx_goods_wid_' . $wid)
				->alias('g')
				->field($field)
				->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id and gs.wid = ' . $wid)
                ->join('LEFT JOIN __GOODS__ AS gg ON g.goods_id = gg.goods_id')
				->where($isBestWhere)
				->limit($categoryIsNew[$goods_where])
				->select(false);
		}

//		热销
		if (array_key_exists($goods_where, $categoryIsHot)) {
			$where = true;

			$time = time();

			$field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.promote_user_rank,gs.practical_num';

			$isBestWhere = array(
				'g.cat_id'				=>	$category_id,
				'g.is_hot'				=>	1,
				'gg.is_hot_start_time'	=>	array('LT', $time),
				'gg.is_hot_end_time'		=>	array('GT', $time),
				'g.is_delete'			=>	0,
				'g.is_on_sale'			=>	1,
				'g.original_img'		=>	array('NEQ', ''),
			);

            if ($is_hide_promote) {
                $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

			$sql = $this
				->table('sdx_goods_wid_' . $wid)
				->alias('g')
				->field($field)
				->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id and gs.wid = ' . $wid)
                ->join('LEFT JOIN __GOODS__ AS gg ON g.goods_id = gs.goods_id')
				->where($isBestWhere)
				->limit($categoryIsHot[$goods_where])
				->select(false);
		}

		if ($where == false){
            $field = 'g.goods_id,g.goods_sn,g.goods_name,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.promote_user_rank,gs.practical_num';

            $isBestWhere = array(
                'g.cat_id'				=>	$category_id,
                'g.is_delete'			=>	0,
                'g.is_on_sale'			=>	1,
                'g.original_img'		=>	array('NEQ', ''),
            );

            if ($is_hide_promote) {
                $isBestWhere['g.promote_end_date'] = array('LT', $promoteTime);
            }

            $sql = $this
                ->table('sdx_goods_wid_' . $wid)
                ->alias('g')
                ->field($field)
                ->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON g.goods_id = gs.goods_id and gs.wid = ' . $wid)
                ->where($isBestWhere)
                ->group('g.goods_id')
                ->select(false);
		}

		if (!isset($sql)) {
			return false;
		}

		$data = $this->query($sql);

		return $data;
	}

	/**
	 * 返回分类所属专题
	 * Created by Jerry
	 * @param $category_id
	 * @return mixed
	 */
	public function findSeminarTemplateCategoryByCategoryId($category_id)
	{
		$time = time();
		$where = array(
			'sc.category_id'	=>	$category_id,
			'st.`status`'		=>	1,
			'st.start_time'		=>	array('LT', $time,),
			'st.end_time'		=>	array('GT', $time,),
			'st.image'			=>	array('NEQ', ''),
		);

		$field = 'st.id,st.`name`,st.image,sc.category_id,sc.goods_where';

		$data = $this
			->alias('sc')
			->field($field)
			->join('LEFT JOIN __SEMINAR_TEMPLATE_PAGES__ AS sp ON sc.template_id = sp.id')
			->join('LEFT JOIN __SEMINAR_TEMPLATES__ AS st ON sp.template_id = st.id')
			->where($where)
			->group('st.id')
			->select();
		return $data;
	}

	/**
	 * 返回商品分类
	 * Created by Jerry
	 * @param $goods_id
	 * @return mixed
	 */
	public function findGoodsByGoodsId($goods_id)
	{
		$where = array(
			'goods_id'	=>	$goods_id,
		);

		$field = 'goods_id, cat_id';

		return $this->table('sdx_goods')->field($field)->where($where)->find();
	}


}